﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pointandclick.Objects
{
    class Draggable: MouseInteractive
    {
        protected bool dragging;
        public bool Dragging {get {return dragging;}}
        Point dragPoint;

        public Draggable(Texture2D image, Point position)
            : base(image, position)
        {
        }

        /// <summary>
        /// Drags the object while the mouse is pressed
        /// </summary>
        /// <param name="mouse"></param>
        /// <param name="gameTime"></param>
        public void Update()
        {
            //Moves the object centered on its dragging point
            if (dragging)
            {
                rectangle.X = Inputs.Mouse.X - dragPoint.X;
                rectangle.Y = Inputs.Mouse.Y - dragPoint.Y;

                if (Inputs.Mouse.LeftButton == ButtonState.Released)
                    dragging = false;
            }

            if (ClickedOn() && !dragging)
            {
                dragging = true;
                //Gets the relative position of the dragging point so that the object moves around it
                dragPoint = new Point(Inputs.Mouse.X - Rectangle.X, Inputs.Mouse.Y - Rectangle.Y);
            }
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font, Color color)
        {
            base.Draw(spriteBatch, color);
        }
    }
}
